﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;

namespace HRMS.Models
{
    public class JobSkillsModels
    {
        public static bool AddJobSkills(JobSkill jobskill)
        {
            try
            {
                using (var jsEntity = new HRMSEntities())
                {
                    jsEntity.JobSkills.AddObject(jobskill);
                    jsEntity.SaveChanges();
                    return true;
                }
            }

            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public static List<JobSkill> GetJobSkills(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var jsEntity = new HRMSEntities())
            {
                return jsEntity.JobSkills.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }
        public static int GetJobSkillsCount()
        {
            using (var jsEntity = new HRMSEntities())
            {
                return jsEntity.JobSkills.Count();
            }
        }

        public static bool UpdateJobSkill([ModelBinder(typeof(CustomModelBinder))]JobSkill jobSkill)
        {
            try
            {
                using (var jsEntity = new HRMSEntities())
                {
                    var jsToEdit = jsEntity.JobSkills.FirstOrDefault(r => r.JobSkillId == jobSkill.JobSkillId);
                    if (jsToEdit != null)
                    {
                        jsToEdit.JobId = jobSkill.JobId;
                        jsToEdit.SkillId = jobSkill.SkillId;
                        
                        //empToEdit.Department.DepartmentName
                    }
                    jsEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteJobSkill(int id)
        {
            try
            {
                using (var jsEntity = new HRMSEntities())
                {
                    jsEntity.JobSkills.DeleteObject(jsEntity.JobSkills.First(e => e.JobSkillId == id));
                    jsEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
    }
}